<%= form_for(@customer) do |f| %>
    <% if @customer.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@customer.errors.count, "error") %> prohibited this customer from being saved:</h2>

          <ul>
            <% @customer.errors.full_messages.each do |message| %>
                <li><%= message %></li>
            <% end %>
          </ul>
        </div>
    <% end %>

    <div class="field">
      <%= f.label :name %><br>
      <%= f.text_field :name, required: true %>
    </div>
    <div class="field">
      <%= f.label :phone %><br>
      <%= f.text_field :phone %>
    </div>
    <div class="field">
      <%= f.label :email %><br>
      <%= f.text_field :email %>
    </div>
    <div class="field">
      <%= f.label :wechat %><br>
      <%= f.text_field :wechat %>
    </div>

    <div class="field">
      <label>省、市、区/县</label>
      <br/>
      <%= f.select :province_id, Region.where(level: 1).map{|r| [r.alias, r.id]} %>
      <%= f.select :city_id, (@customer.city_id.present? ? Region.where(parent_code: @customer.city.parent_code).map{|r| [r.alias, r.id]} : []) %>
      <%= f.select :district_id, (@customer.district_id.present? ? Region.where(parent_code: @customer.district.parent_code).map{|r| [r.alias, r.id]} : []) %>
    </div>

    <div class="field">
      <%= f.label :street %><br>
      <%= f.text_field :street %>
    </div>

    <div class="field">
      <%= f.label :address %><br>
      <%= f.text_field :address %>
    </div>

    <div class="field">
      <label>车辆</label>
      <%= f.check_box :has_car %>
    </div>

    <div id="car_fields" <%=  'style="display: none;"'.html_safe unless @customer.has_car %>>
    <%= f.fields_for :car, @customer.car || Car.new do |ff| %>
        <%= ff.check_box :_destroy, style: 'display: none;' %>

        <div class="field">
         <%= ff.label :id_card_no %><br/>
         <%= ff.text_field :id_card_no %>
        </div>
        <div class="field">
          <%= ff.label :license_plate_no %><br/>
          <%= ff.text_field :license_plate_no %>
        </div>
        <div class="field">
          <%= ff.label :body_no %><br/>
          <%= ff.text_field :body_no %>
        </div>
        <div class="field">
          <%= ff.label :engine_no %><br/>
          <%= ff.text_field :engine_no %>
        </div>
        <div class="field">
          <%= ff.label :registered_at %><br/>
          <%= ff.text_field :registered_at, value: ff.object.registered_at.try(:strftime, '%F'), readonly: false %>
        </div>
        <div class="field">
          <%= ff.label :last_insurance_company %><br/>
          <%= ff.text_field :last_insurance_company %>
        </div>
    <% end %>
    </div>

    <div class="actions m-t-10">
      <%= link_to '取消', customers_path, class: 'btn btn-default' %>
      <%= f.submit '保存', class: 'btn btn-primary' %>
    </div>
<% end %>

<% content_for :javascript do %>
<script>
  $(function(){
      $('form').validate();

      var subSelect = function(select, subSelect){
          $(select).change(function(){
              var $this = $(this);
              var parentId = $this.val();

              $.get('/regions/' + parentId + '.json', function(region){
                  var options = '';
                  for(var i = 0; i < region.children.length; i++){
                      var r = region.children[i];
                      options += '<option value="'+ r.id +'">' + r.alias + '</option>';
                  }

                  $(subSelect).empty().append(options);
                  $(subSelect).change();
              });
          });
      };

      var selectedProvince = <%= @customer.province_id || 'null' %>;
      subSelect('#customer_city_id', '#customer_district_id');
      subSelect('#customer_province_id', '#customer_city_id');

      if(!selectedProvince){
        $('#customer_province_id').change();
      }


      $('#customer_has_car').change(function(){
          if($(this).prop('checked')){
              $('#customer_car_attributes__destroy').prop('checked', false)
              $('#car_fields').show();
          } else {
              $('#customer_car_attributes__destroy').prop('checked', true)
              $('#car_fields').hide();
          }
      }).change();

      $('#customer_car_attributes_registered_at').datepicker({
          format: "yyyy-mm-dd",
          language: "zh-CN",
          weekStart: 1,
          autoclose: true,
          todayHighlight: true
      });
  });
</script>
<% end %>
